//引入头文件
let url_js = require("../../utils/url.js");
var common_js = require("../../utils/common.js");
var const_js = require("../../utils/const.js");
var user_js = require("../../utils/user.js");
var sys_js = require("../../utils/system.js");
var ubt_js = require("../../utils/ubt.js");
var name_js = require("../../utils/ubtButtonName.js");
var cart_js = require("../../utils/cart.js");
var page_js = require("../../utils/pageOptions.js");
var api = require("../../utils/apiRequest.js");
let notificationCenter = require('../../thridLib/WxNotificationCenter.js');

const app = getApp()
Page({
  data: {
    sysInfo: null,
    userInfo: null,
    canCusNav: true,
    pageScrollTop: 0,
    isDiscountShow: false,
    discountInfo: null,
    containerTouchmove: '',
    contentStatus: -1, //2已绑定门店
    header: {
      title: '大牛惠生活',
      headerbg: 'linear-gradient(#FF5C00,#FF7E00);',
      hiddenBack: true
    },
    limitTimerObj: { //限时抢购计时器对象
      timer: null,
      isEnd: false,
      endTime: '',
      item: null
    },
    bannerList: [], //轮播图
    cmsInfo: null, //cms广告
    categoryInlineCWidth: 750,
    categoryList: [], //分类
    limitSaleList: [], //限时抢购list
    allGoodsCategoryList: [],
    allGoodsCategorySelectIndex: 0,
    allGoodsCategoryGoodsList: null, //根据省id存储县区数组,二维数组与map互换
    //当前选中的分类商品
    allGoodsCategorySelctedGoodsInfo: {
      pageInfo: {
        curPage: 1,
        pageSize: 10,
        noMore: false
      },
      itemList: []
    },
    goodsPropertyInfo: {
      pageShow: false, //页面展示
      dismiss: false, //手动设置隐藏，当属性页面隐藏完成后，pageShow设置false
    },
    addCartSelctedGoodsInfo: null, //当前添加购物车的商品，只有当有多种规格时有数据,
    shareInfo: null,//分享信息
  },

  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.注册通知
    this.registNotification();
    //2.选择角色
    this.chooseRoleToPlay();
    //3.处理
    if (user_js.hasRole()) {
      //3.1.初始化数据
      this.initData(options);
      //3.2.网路请求获取数据
      this.getRemoteDatas(false);
    }
  },

  //注册通知，当自提点切换的时候，更新数据
  registNotification: function() {
    var that = this;
    notificationCenter.addNotification(const_js.changeShopSucessNoti, that.changedShop, that);
  },

  //自提点切换
  changedShop: function() {
    //1.更新userInfo
    let tempUserInfo = user_js.userInfo();
    this.setData({
      userInfo: tempUserInfo
    });
    //2.刷新数据
    this.getRemoteDatas(true);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //更新购物车数量，setTabBarBadge受限制
    cart_js.updateTabBarCount();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    //移除通知
    var that = this;
    notificationCenter.removeNotification(const_js.changeShopSucessNoti, that);

    //如果计时器的话清除
    if (this.data.limitTimerObj.timer) {
      clearTimeout(this.data.limitTimerObj.timer);
    }
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  // 监听用户滑动页面事件
  onPageScroll: function(e) {
    var topY = e.scrollTop + "px";
    this.setData({
      pageScrollTop: topY
    });
  },

  //按钮点击事件
  //禁止滚动
  preventTouchMove: function() {},

  //初始化数据
  initData: function(options) {
    //初始化购物车角标
    cart_js.initCartTabBarCount();
    //set data
    this.setData({
      canCusNav: app.globalData.canCusNav,
      sysInfo: sys_js.systemInfo()
    });

    //清除状态
    var tmpPageOptions = page_js.pageOptionsInfo();
    if (tmpPageOptions) {
      page_js.clearPageOptionsInfo();
    }
  },

  //计算类别的宽度
  updateCalculateCategoryWidth: function(categoryList) {
    //计算类别宽度
    var width = 750;
    if (categoryList.length > 8) {
      width += (Math.ceil((categoryList.length - 8) / 2) * 187.5);
      this.setData({
        categoryInlineCWidth: width,
      });
    }
  },

  //选择用户角色（购物，团长，供应商）
  chooseRoleToPlay: function() {
    let tempUserInfo = user_js.userInfo();
    if (!tempUserInfo || tempUserInfo.role == -1) {
      wx.reLaunch({
        url: '../whoAreYou/whoAreYou'
      });
    } else {
      var tmpContentSt = this.data.contentStatus;
      if (tempUserInfo.ztd.shopName) {
        tmpContentSt = 2;
      }
      this.setData({
        contentStatus: tmpContentSt,
        userInfo: tempUserInfo
      });
    }
  },

  //网络请求获取数据
  getRemoteDatas: function(pullDownRefresh) {

    //Promise.all
    let homeFlushSalesData = {
      cityId: this.data.userInfo.ztd.cityId,
      code: this.data.userInfo.code,
      shopId: this.data.userInfo.ztd.id
    };
    let shareData = {
      cityId: this.data.userInfo.ztd.cityId,
      code: this.data.userInfo.code,
      shopId: this.data.userInfo.ztd.id,
      type: 0
    };

    //1.获取轮播图，分类，领券
    //2.获取限时抢购
    //3.获取首页分类
    //4.获取分享数据
    Promise.all([api.wxRequest(url_js.homeBannerIconUrl, {}), api.wxRequest(url_js.homeFlushSalesUrl, homeFlushSalesData), api.wxRequest(url_js.homeFrontCategoriesUrl, homeFlushSalesData), api.wxRequest(url_js.homeShareConfigUrl, shareData)]).then(results => {
      this.resetAllData(pullDownRefresh);
      this.handleHomeBannerIconData(results[0]);
      this.handleHomeFlushSalesData(results[1]);
      this.handleHomeFrontCategoriesData(results[2]);
      this.handleHomeShareConfigData(results[3])
      common_js.hidePageLoading(pullDownRefresh);
    }).catch(err => {
      common_js.hidePageLoading(pullDownRefresh);
      common_js.log(err);
    });
  },

  //重置数据
  //刷新重置数据
  resetAllData(pullDownRefresh) {
    if (!pullDownRefresh) return;

    var tmpBannerList = this.data.bannerList;
    tmpBannerList = [];

    var tmpCategoryList = this.data.categoryList;
    tmpCategoryList = [];

    var tmpLimitSaleList = this.data.limitSaleList;
    tmpLimitSaleList = [];

    var tmpAllGoodsCategoryList = this.data.allGoodsCategoryList;
    tmpAllGoodsCategoryList = [];

    var tmpAllGoodsCategoryGoodsList = this.data.allGoodsCategoryGoodsList;
    tmpAllGoodsCategoryGoodsList = [];

    //当前选中的分类商品
    var tmpAllGoodsCategorySelctedGoodsInfo = this.data.allGoodsCategorySelctedGoodsInfo;
    tmpAllGoodsCategorySelctedGoodsInfo.pageInfo.curPage = 1;
    tmpAllGoodsCategorySelctedGoodsInfo.pageInfo.pageSize = 10;
    tmpAllGoodsCategorySelctedGoodsInfo.pageInfo.noMore = false;
    tmpAllGoodsCategorySelctedGoodsInfo.itemList = [];

    var tmpTimer = this.data.limitTimerObj;
    tmpTimer.timer = null;
    tmpTimer.isEnd = false;
    tmpTimer.endTime = '';
    tmpTimer.item = null;

    this.setData({
      limitTimerObj: tmpTimer,
      bannerList: tmpBannerList,
      cmsInfo: null,
      categoryList: tmpCategoryList,
      limitSaleList: tmpLimitSaleList, //限时抢购list
      allGoodsCategoryList: tmpAllGoodsCategoryList,
      allGoodsCategorySelectIndex: 0,
      categoryInlineCWidth: 750,
      allGoodsCategoryGoodsList: tmpAllGoodsCategoryGoodsList, //根据省id存储县区数组,二维数组与map互换
      allGoodsCategorySelctedGoodsInfo: tmpAllGoodsCategorySelctedGoodsInfo,
      addCartSelctedGoodsInfo: null
    });
  },

  //处理返回的轮播图数据
  handleHomeBannerIconData: function(res) {
    //轮播图
    var tmpBannerList = res.result.bannerVoList;
    //首页分类
    var tmpCateList = res.result.iconVoList;
    //cms活动
    var tmpCmsInfo = res.result.operateTilingVo;
    //弹窗领券
    var tmpOperateAdvertVo = res.result.operateAdvertVo;
    this.setData({
      bannerList: tmpBannerList,
      categoryList: tmpCateList,
      cmsInfo: tmpCmsInfo
    });
    if (tmpOperateAdvertVo) {
      this.setData({
        discountInfo: tmpOperateAdvertVo,
        isDiscountShow: true,
        containerTouchmove: ''
      });
    }

    //跟新categoryList宽度
    this.updateCalculateCategoryWidth(tmpCateList);
  },

  //处理返回的轮播图数据
  handleHomeFlushSalesData: function(res) {
    if (!res.result || !res.result.productList) return;

    var tmpProList = res.result.productList;
    if (tmpProList.length == 0) return;
    //执行限时抢购倒计时
    var tmpLimitTimerObj = this.data.limitTimerObj;
    tmpLimitTimerObj.endTime = new Date().getTime() + res.result.expire * 1000;
    this.setData({
      limitTimerObj: tmpLimitTimerObj,
      limitSaleList: tmpProList
    });
    //
    clearTimeout(this.data.limitTimerObj.timer);
    this.countDownLimitTimer();
  },

  //处理返回的分类数据
  handleHomeFrontCategoriesData: function(res) {
    var tmpCateList = res.result;
    if (tmpCateList.length > 0) {
      this.setData({
        allGoodsCategoryList: tmpCateList
      });
      //请求分类第一个商品数据
      let firstCateItem = tmpCateList[0];
      this.requestToGetHomeFrontCategoryProduct(firstCateItem.id, 1, 10);
    }
  },

  //处理返回的分享数据
  handleHomeShareConfigData: function(res) {
    this.setData({
      shareInfo: res.result
    });
  },

  //获取横向滚动分类列表的位置
  queryAllGoodsCategoryCPosition: function() {
    let query = wx.createSelectorQuery()
    query.select(".all-goods-category-c").boundingClientRect(function(res) {}).exec();
  },

  //首页分类对应的商品
  requestToGetHomeFrontCategoryProduct: function(categoryId, curPage, pageSize) {
    let data = {
      categoryId: categoryId,
      cityId: this.data.userInfo.ztd.cityId,
      code: this.data.userInfo.code,
      curPage: curPage,
      pageSize: pageSize,
      shopId: this.data.userInfo.ztd.id
    };
    api.wxRequest(url_js.homeFrontCategoryProductUrl, data).then(res => {
      this.handleHomeFrontCategoryProductData(res, curPage, categoryId);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理分类对应的商品
  handleHomeFrontCategoryProductData: function(res, curPage, categoryId) {
    var tmpGoodsMap = new Map(this.data.allGoodsCategoryGoodsList);
    var tmpGoodsInfo = null;
    if (tmpGoodsMap.get(categoryId)) {
      tmpGoodsInfo = tmpGoodsMap.get(categoryId);
    } else {
      tmpGoodsInfo = {
        pageInfo: {
          curPage: 1,
          pageSize: 10,
          noMore: false
        },
        itemList: []
      }
    }
    //page
    if (res.page.pages <= curPage) {
      tmpGoodsInfo.pageInfo.noMore = true;
    } else {
      tmpGoodsInfo.pageInfo.curPage++;
    }
    //list
    if (res.result.length > 0) {
      tmpGoodsInfo.itemList = tmpGoodsInfo.itemList.concat(res.result);
    }
    //set data
    tmpGoodsMap.set(categoryId, tmpGoodsInfo);
    this.setData({
      allGoodsCategorySelctedGoodsInfo: tmpGoodsInfo,
      allGoodsCategoryGoodsList: [...tmpGoodsMap]
    });
  },

  //增加购物车
  requestToAddCart: function(skuItem, productId, skuId, num) {
    let data = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      num: num,
      productId: productId,
      shopId: this.data.userInfo.ztd.id,
      skuId: skuId
    };
    api.wxRequest(url_js.cartAddUrl, data).then(res => {
      this.handleAddCartData(res, skuItem, skuId, num);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理添加购物车
  handleAddCartData: function(res, skuItem, skuId, num) {
    cart_js.addCart(skuItem, skuId, num);
    common_js.showToast("添加购物车成功");
  },

  //新用户领券
  requestToCouponPick: function(couponPackageId) {
    let data = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      couponPackageId: couponPackageId,
      shopId: this.data.userInfo.ztd.id,
    };
    api.wxRequest(url_js.couponPickUrl, data).then(res => {
      this.handleCouponPickData(res);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理添加购物车
  handleCouponPickData: function(res) {
    common_js.showToast("领取成功");
    this.setData({
      discountInfo: null
    });
  },

  //选择自提点
  onPosition: function() {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexZtd);

    wx.navigateTo({
      url: '../indexSearchShop/indexSearchShop'
    })
  },

  //确定自提点
  ensurePosition: function() {

  },

  //点击banner
  onBanner: function(e) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexBanner);

    var index = e.currentTarget.dataset.index;
    var item = this.data.bannerList[index];
    //去掉头部的pages/
    var tmpLink = '../' + item.linkUrl;
    //跳转页面
    wx.navigateTo({
      url: tmpLink,
      fail: res => {
        common_js.showToast("跳转失败");
      }
    })
  },

  //点击cms占位图
  onCmsActivity: function() {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexCms);

    //去掉头部的pages/
    var tmpLink = '../' + this.data.cmsInfo.linkUrl;
    //跳转页面
    wx.navigateTo({
      url: tmpLink,
      fail: res => {
        common_js.showToast("跳转失败");
      }
    });
  },

  //点击类别
  onCategoryItem: function(e) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexCg);

    var index = e.currentTarget.dataset.index;
    var item = this.data.categoryList[index];
    if (item.linkType == 3) {
      wx.navigateTo({
        url: '../groupBuyingList/groupBuyingList'
      })
      return;
    }
    wx.navigateTo({
      url: '../goodsList/goodsList?item=' + common_js.jsonStringify(item)
    })
  },

  //搜索
  onSearch: function(e) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexSearch);

    wx.navigateTo({
      url: '../searchIndex/searchIndex'
    })
  },

  //更多限时抢购
  onLimitMore: function() {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexLimitMore);

    wx.navigateTo({
      url: '../buyLimit/buyLimit'
    });
  },

  //添加限时抢购商品
  onLimitAdd: function(e) {
    if (!this.checkIsToLoginIndex()) return;

    var index = e.currentTarget.dataset.index;
    var item = this.data.limitSaleList[index];
    if (item.skuList && item.skuList.length > 1) {
      if (item.dimensions.length == 0) {
        common_js.showToast("商品多规格属性不存在！");
        return;
      }
      //多规格,选择规格加入购物车
      //展示多规格商品
      var tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
      tmpGoodsPropertyInfo.pageShow = true;
      this.setData({
        addCartSelctedGoodsInfo: item,
        goodsPropertyInfo: tmpGoodsPropertyInfo,
        containerTouchmove: 'preventTouchMove'
      });
    } else {
      //单一规格,直接添加购物车
      if (item.skuList == 0) {
        common_js.showToast("商品sku不存在");
        return;
      }
      //限时抢购单独判断
      if (item.type == 2) {
        var skuItem = item.skuList[0];
        var cartLimitCount = cart_js.limitSkuCountWithSkuId(skuItem.id);
        var isAlert = (cartLimitCount == 0) ? (item.count > skuItem.userBuyLimit) : (cartLimitCount >= skuItem.userBuyLimit);
        if (isAlert) {
          common_js.showModal("每人限购" + skuItem.userBuyLimit + "件，超过的件数将以原价支付！", null, true, null, "我知道了", () => {
            this.requestToAddCart(item.skuList[0], item.id, item.skuList[0].id, 1);
          })
          return;
        }
      }
      this.requestToAddCart(item.skuList[0], item.id, item.skuList[0].id, 1);
    }

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexLimitGoodsAdd);
  },

  //查看限时商品详情
  onBuyLimitItem: function(e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.limitSaleList[index];
    wx.navigateTo({
      url: '../goodsDetails/goodsDetails?productId=' + item.id
    });
  },

  //查看商品详情
  onGoodsDetail: function(e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.allGoodsCategorySelctedGoodsInfo.itemList[index];
    wx.navigateTo({
      url: '../goodsDetails/goodsDetails?productId=' + item.id
    })
  },

  //全部分类
  onAllGoodsCategery: function(e) {
    var index = e.currentTarget.dataset.index;
    if (this.data.allGoodsCategorySelectIndex == index) return;
    var item = this.data.allGoodsCategoryList[index];
    //判断是否已经存在
    var tmpGoodsMap = new Map(this.data.allGoodsCategoryGoodsList);
    var tmpGoodsSelectInfo = tmpGoodsMap.get(item.id);
    if (tmpGoodsSelectInfo) {
      this.setData({
        allGoodsCategorySelctedGoodsInfo: tmpGoodsSelectInfo,
        allGoodsCategorySelectIndex: index
      });
    } else {
      //请求当前分类下的商品
      this.requestToGetHomeFrontCategoryProduct(item.id, 1, 10);
      //set data
      this.setData({
        allGoodsCategorySelectIndex: index
      });
    }
  },

  //点击分类详情添加
  onAllCategoryGoodsAdd: function(e) {
    if (!this.checkIsToLoginIndex()) return;

    //2.加入购物车
    var index = e.currentTarget.dataset.index;
    var item = this.data.allGoodsCategorySelctedGoodsInfo.itemList[index];
    if (item.skuList && item.skuList.length > 1) {
      if (item.dimensions.length == 0) {
        common_js.showToast("商品多规格属性不存在！");
        return;
      }
      //多规格,选择规格加入购物车
      //展示多规格商品
      var tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
      tmpGoodsPropertyInfo.pageShow = true;
      this.setData({
        addCartSelctedGoodsInfo: item,
        goodsPropertyInfo: tmpGoodsPropertyInfo,
        containerTouchmove: 'preventTouchMove'
      });
    } else {
      //单一规格,
      if (item.skuList == 0) {
        common_js.showToast("商品sku不存在");
        return;
      }
      //限时抢购单独判断
      if (item.type == 2) {
        var skuItem = item.skuList[0];
        var cartLimitCount = cart_js.limitSkuCountWithSkuId(skuItem.id);
        var isAlert = (cartLimitCount == 0) ? (item.count > skuItem.userBuyLimit) : (cartLimitCount >= skuItem.userBuyLimit);
        if (isAlert) {
          common_js.showModal("每人限购" + skuItem.userBuyLimit + "件，超过的件数将以原价支付！", null, true, null, "我知道了", () => {
            this.requestToAddCart(item.skuList[0], item.id, item.skuList[0].id, 1);
          })
          return;
        }
      }
      this.requestToAddCart(item.skuList[0], item.id, item.skuList[0].id, 1);
    }

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexCategoryGoodsAdd);
  },

  //查看登录状态，未登录直接跳转登录
  checkIsToLoginIndex: function() {
    if (!user_js.isLogin()) {
      wx.navigateTo({
        url: '../loginIndex/loginIndex'
      });
      return false;
    }
    return true;
  },

  //关闭领券弹窗
  onDiscountClose: function() {
    this.setData({
      isDiscountShow: false,
      containerTouchmove: ''
    });
  },

  //去领券
  onDiscount: function() {
    if (!this.checkIsToLoginIndex()) return;

    this.setData({
      isDiscountShow: false,
      containerTouchmove: '',
    });

    //判断类型
    if (this.data.discountInfo.linkType == 2) {
      //领券
      this.requestToCouponPick(this.data.discountInfo.linkParam);
    } else {
      //去掉头部的pages/
      var tmpLink = '../' + this.data.discountInfo.linkUrl;
      //跳转页面
      wx.navigateTo({
        url: tmpLink,
        fail: res => {
          common_js.showToast("跳转失败");
        }
      })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.allGoodsCategorySelctedGoodsInfo.pageInfo.noMore) return;
    //1. request
    var item = this.data.allGoodsCategoryList[this.data.allGoodsCategorySelectIndex];
    if (item) {
      this.requestToGetHomeFrontCategoryProduct(item.id, this.data.allGoodsCategorySelctedGoodsInfo.pageInfo.curPage, this.data.allGoodsCategorySelctedGoodsInfo.pageInfo.pageSize);
    }
  },

  //商品属性选择确定
  onGoodsPropertyEnsure: function(e) {
    //消失
    this.onGoodsPropertyHidden();
    //添加购物车
    if (e) {
      var item = e.detail;
      //限时抢购单独判断
      if (this.data.addCartSelctedGoodsInfo.type == 2) {
        var skuItem = item.sku;
        var cartLimitCount = cart_js.limitSkuCountWithSkuId(skuItem.id);
        var isAlert = (cartLimitCount == 0) ? (item.count > skuItem.userBuyLimit) : (cartLimitCount >= skuItem.userBuyLimit);
        if (isAlert) {
          common_js.showModal("每人限购" + skuItem.userBuyLimit + "件，超过的件数将以原价支付！", null, true, null, "我知道了", () => {
            this.requestToAddCart(item.sku, this.data.addCartSelctedGoodsInfo.id, item.sku.id, item.count);
          })
          return;
        }
      }
      this.requestToAddCart(item.sku, this.data.addCartSelctedGoodsInfo.id, item.sku.id, item.count);
    }
  },

  //商品属性选择
  onGoodsPropertyHidden: function(e) {
    var tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.pageShow = false;
    tmpGoodsPropertyInfo.dismiss = false,
      this.setData({
        containerTouchmove: '',
        goodsPropertyInfo: tmpGoodsPropertyInfo
      });
  },

  //隐藏商品属性页面
  onHidePropertyPageView: function(e) {
    var tmpGoodsPropertyInfo = this.data.goodsPropertyInfo;
    tmpGoodsPropertyInfo.dismiss = true;
    this.setData({
      goodsPropertyInfo: tmpGoodsPropertyInfo
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (this.data.contentStatus != 2) {
      wx.stopPullDownRefresh();
      return
    }
    // //3.request
    this.getRemoteDatas(true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexShare);

    return this.shareAppMessage();
  },

  //获取分享的AppMessage
  shareAppMessage: function() {
    let tmpPath = '/pages/index/index';
    return {
      title: this.data.shareInfo ? this.data.shareInfo.title : '大牛惠生活',
      path: tmpPath,
      imageUrl: this.data.shareInfo ? this.data.shareInfo.href : ''
    }
  },

  // 限时抢购计时器**********
  countDownLimitTimer() { //倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    var tmpTimerObj = this.data.limitTimerObj;
    let endTime = tmpTimerObj.endTime;;

    // 对结束时间进行处理渲染到页面
    let obj = null;
    // 如果活动未结束，对时间进行处理
    if (endTime - newTime > 0) {
      let time = (endTime - newTime) / 1000;
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24));
      let dayHour = (day > 0) ? day * 24 : 0;
      let hou = dayHour + parseInt(time % (60 * 60 * 24) / 3600);
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
      obj = {
        day: common_js.timeFormat(day),
        hou: common_js.timeFormat(hou),
        min: common_js.timeFormat(min),
        sec: common_js.timeFormat(sec)
      }
    } else { //活动已结束，全部设置为'00'
      obj = {
        day: '00',
        hou: '00',
        min: '00',
        sec: '00'
      }
      tmpTimerObj.isEnd = true;
    }
    tmpTimerObj.item = obj;
    //1s调用一次
    var that = this;
    var timerItem = setTimeout(function() {
      that.countDownLimitTimer()
    }, 1000);
    //定时清除
    clearTimeout(this.data.limitTimerObj.timer);
    if (tmpTimerObj.isEnd) {
      clearTimeout(timerItem);
      return;
    } else {
      tmpTimerObj.timer = timerItem;
    }
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      limitTimerObj: tmpTimerObj
    })
  }
  // 限时抢购计时器**********
})